package com.dashu.core.service;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.core.result.PageEnty;
import com.dashu.base.utils.CopyUtil;
import com.dashu.core.dto.JurisdictionDTO;
import com.dashu.core.handler.VerificationException;
import com.dashu.core.model.JurisdictionRule;
import com.dashu.core.model.QJurisdictionRule;
import com.dashu.core.vo.JurisdictionVO;
import com.dashu.core.vo.UpdateEntityVO;
import com.querydsl.core.BooleanBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.persistence.FlushModeType;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class JurisdictionService extends DataService {

    @Transactional
    public JurisdictionDTO add(JurisdictionVO jurisdictionVO) {
        JurisdictionRule jurisdictionRule = CopyUtil.copyEnty(jurisdictionVO, JurisdictionRule.class);
        try {
            entityManager.persist(jurisdictionRule);
//            entityManager.flush();
            entityManager.setFlushMode(FlushModeType.AUTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new VerificationException(ReplRyCode.DATA_NOT_INVALID, ReplRyCode.DATA_NOT_INVALID.getMessage());
        }
        JurisdictionDTO jurisdictionDTO = CopyUtil.copyEnty(jurisdictionRule, JurisdictionDTO.class);
        return jurisdictionDTO;

    }

    @Transactional
    public boolean deleteByJurisdictionRuleId(long jurisdictionRuleId) {
        QJurisdictionRule qJurisdictionRule = QJurisdictionRule.jurisdictionRule;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qJurisdictionRule.id.eq(jurisdictionRuleId));
        JurisdictionRule JurisdictionRule;

            queryFactory.select();
            JurisdictionRule = queryFactory
                    .select(qJurisdictionRule)
                    .from(qJurisdictionRule)
                    .where(booleanBuilder.getValue()).fetchOne();
            if (JurisdictionRule == null) return false;
            entityManager.remove(JurisdictionRule);
//            entityManager.flush();
            entityManager.setFlushMode(FlushModeType.AUTO);
        return true;

    }

    @Transactional
    public JurisdictionDTO updataByJurisdictionRuleId(UpdateEntityVO<JurisdictionVO> updateJurisdictionVO) {
        QJurisdictionRule qJurisdictionRule = QJurisdictionRule.jurisdictionRule;
        JurisdictionVO jurisdictionVO = updateJurisdictionVO.getEntityVO();
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qJurisdictionRule.id.eq(updateJurisdictionVO.getId()));
        JurisdictionRule jurisdictionRule;
        try {
            jurisdictionRule = queryFactory
                    .select(qJurisdictionRule)
                    .from(qJurisdictionRule)
                    .where(booleanBuilder.getValue()).fetchOne();
            if (jurisdictionRule == null) return null;
            jurisdictionRule.setRuleContent(jurisdictionVO.getRuleContent());
            jurisdictionRule.setRuleName(jurisdictionVO.getRuleName());
            jurisdictionRule.setUpdateTime(new Date().getTime());
            jurisdictionRule = entityManager.merge(jurisdictionRule);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
//        entityManager.flush();
        entityManager.setFlushMode(FlushModeType.AUTO);
        JurisdictionDTO JurisdictionDTO = CopyUtil.copyEnty(jurisdictionRule, JurisdictionDTO.class);
        return JurisdictionDTO;

    }

    public List<JurisdictionDTO> findByBaseId(PageEnty<Long> basrVo) {
        QJurisdictionRule qJurisdictionRule = QJurisdictionRule.jurisdictionRule;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qJurisdictionRule.baseId.eq(basrVo.getBaseId()));
        List<JurisdictionRule> jurisdictionRules = queryFactory
                .select(qJurisdictionRule)
                .from(qJurisdictionRule)
                .where(booleanBuilder.getValue())
                .fetch();
        List<JurisdictionDTO> recycleDTOS = CopyUtil.copyList(jurisdictionRules, JurisdictionDTO.class);
        return recycleDTOS;

    }

    public JurisdictionDTO findByJurisdictionRuleId(long jurisdictionRuleId) {
        QJurisdictionRule qJurisdictionRule = QJurisdictionRule.jurisdictionRule;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qJurisdictionRule.id.eq(jurisdictionRuleId));
        JurisdictionRule jurisdictionRule = queryFactory
                .select(qJurisdictionRule)
                .from(qJurisdictionRule)
                .where(booleanBuilder.getValue()).fetchOne();
        if (jurisdictionRule == null) return null;
        JurisdictionDTO JurisdictionDTO = CopyUtil.copyEnty(jurisdictionRule, JurisdictionDTO.class);
        return JurisdictionDTO;

    }


}
